package com.webcams.core.resorts;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;

import com.webcams.core.utils.Utils;

public class Resort {
	private String name;
	private List<Webcam> webcams;
	private List<Meteo> meteo;
	private int id;
	private int dep;
	private float lat;
	private float lon;
	private int mapLastUpdate;
	private int mapSize;
	private int logoLastUpdate;
	private int lastDownload;
	
	public static String distMapFolder = "plans/";
	public static String localMapFolder = "maps/";
	
	public static String distLogoFolder = "logos/";
	public static String localLogoFolder = "logos/";
	
	public static String distWebcamsFolder = "webcams/";
	public static String localWebcamsFolder = "webcams/";

	public static String urlWebAcces = "http://thibaud-perso.com/webcams/";
	
	
	public Resort(String name, int id, int dep, int webcamsSize, float lat, float lon, 
			int mapSize, int mapLastUpdate, int logoLastUpdate, int lastDownload){
		this.name = name;
		this.id = id;
		this.dep = dep;
		this.lat = lat;
		this.lon = lon;
		this.mapSize = mapSize;
		this.mapLastUpdate = mapLastUpdate;
		this.logoLastUpdate = logoLastUpdate;
		this.lastDownload = lastDownload;
		
		webcams = new ArrayList<Webcam>();
		meteo = new ArrayList<Meteo>();
		
		for(int i = 1; i <= webcamsSize; i++) {
			String fileName = id+"-"+i+".jpg";
			webcams.add(new Webcam(Resort.urlWebAcces+Resort.distWebcamsFolder+"high/"+fileName,
					Resort.urlWebAcces+Resort.distWebcamsFolder+"small/"+fileName));
		}
		
	}

	public void addMeteo(Meteo m){
		meteo.add(m);
	}
	
	public String getName(){
		return name;
	}
	
	public Webcam getWebcam(int pos){
		if (pos < webcams.size()) {
			return webcams.get(pos);
		}
		return webcams.get(0);
	}
	
	public Meteo getMeteo(int pos){
		if(pos < meteo.size())
			return meteo.get(pos);
		return meteo.get(0);
	}

	public int getDep() {
		return dep;
	}

	public float getLat() {
		return lat;
	}

	public float getLon() {
		return lon;
	}

	public int getId() {
		return id;
	}

	public String getMapFileName() {
		return "plan_"+id+".jpg";
	}
	
	public String getLogoFileName() {
		return "logo_"+id+".jpg";
	}
	
	public String getMapDistUrl() {
		return urlWebAcces+distMapFolder+getMapFileName();
	}
	
	@Override
	public String toString(){
		return getName();
	}
	
	public File getMapLocalFile(Context context){
		return new File(context.getExternalFilesDir(null), localMapFolder+getMapFileName());
	}
	
	public File getLogoLocalFile(Context context){
		return new File(context.getExternalFilesDir(null), localLogoFolder+getLogoFileName());
	}

	public int getMapSize() {
		return mapSize;
	}

	public int getMapLastUpdate() {
		return mapLastUpdate;
	}

	public int getLogoLastUpdate() {
		return logoLastUpdate;
	}
	
	public int getLastDownload() {
		return lastDownload;
	}

	public boolean newMapAvailable(Context context) {
		if(getMapLocalFile(context) != null) {
			return getMapLastUpdate() > (getMapLocalFile(context).lastModified()/1000);
		}
		return true;
	}
	
	public boolean newLogoAvailable(Context context) {
		if(getLogoLocalFile(context) != null) {
			return getLogoLastUpdate() > (getLogoLocalFile(context).lastModified()/1000);
		}
		return true;
	}
	
	
	public double getDistanceFrom(double latitude, double longitude) {
		return Utils.distance(latitude, longitude, getLat(), getLon());
	}

	public int getWebcamsSize() {
		return webcams.size();
	}

	public double getAngleFrom(double latitude, double longitude) {
		return Utils.angle(latitude, longitude, getLat(), getLon());
	}
	
}